package com.ptmall.app.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.ptmall.app.App;


/**
 * Created by Shelwee on 14-5-8.
 */
public class NetWorkUtils {
    
    //表示当前网络没有连接
    public static int NET_WORK_STATE_UN_CONNECT = 0;
    
    //当前网络为wifi
    public static int NET_WORK_STATE_WIFI = 1;
    
    //当前网络为手机流量连接
    public static int NET_WORK_STATE_MOBILE = 2;


    private static NetWorkUtils INSTANCE;

    private Context mContext;
    private ConnectivityManager connectivityManager;
    private NetworkInfo networkInfo;

    public NetWorkUtils() {
        this.mContext = App.getInstance().getApplicationContext();
        connectivityManager = (ConnectivityManager) this.mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = connectivityManager.getActiveNetworkInfo();
    }

    public static NetWorkUtils getInstance(){
        if (null == INSTANCE) {
            INSTANCE = new NetWorkUtils();
        }
        return INSTANCE;
    }


    public boolean isConnected() {
        return networkInfo != null && networkInfo.isConnected();
    }


    public static boolean isConnectedNet() {
        return getInstance().isConnected();
    }



    /**
     * 获取当前网络状态
     * @return link NET_WORK_STATE_UN_CONNECT、NET_WORK_STATE_WIFI、NET_WORK_STATE_MOBILE
     */
    public int getNetType() {
        if (!isConnected()) {
            return NET_WORK_STATE_UN_CONNECT;
        }
        int type = networkInfo.getType();
        if (type == ConnectivityManager.TYPE_WIFI) {
            return NET_WORK_STATE_WIFI;
        } else {
            return NET_WORK_STATE_MOBILE;
        }
    }

}